//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.base.entity;

import cn.hutool.json.JSONUtil;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;

public class ResultData extends HashMap<String, Object> {
    public static final String CODE_KEY = "code";
    public static final String DATA_KEY = "data";
    public static final String MSG_KEY = "msg";
    public static final String RESULT_KEY = "result";

    public ResultData() {
    }

    public static ResultData build() {
        return new ResultData();
    }

    public ResultData code(HttpStatus code) {
        return this.add("result", code == HttpStatus.OK).add("code", code.value());
    }

    public ResultData code(String code) {
        return this.add("result", code.equalsIgnoreCase(HttpStatus.OK.toString())).add("code", code);
    }

    public ResultData msg(String msg) {
        return this.add("msg", msg);
    }

    public ResultData data(Object data) {
        return this.add("data", data);
    }

    public ResultData success() {
        return this.code(HttpStatus.OK);
    }

    public ResultData success(Object data) {
        return this.success().data(data);
    }

    public ResultData success(String msg, Object data) {
        return this.success().msg(msg).data(data);
    }

    public ResultData error() {
        return this.code(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public ResultData error(String msg) {
        return this.error().msg(msg);
    }

    public ResultData add(String key, Object value) {
        this.put(key, value);
        return this;
    }

    public <T> T getData(Class<T> c) {
        Object obj = this.get("data");
        return obj instanceof Map ? JSONUtil.toBean(JSONUtil.toJsonStr(obj), c) : (T) obj;
    }

    public String getMsg() {
        return this.get("msg") + "";
    }

    public int getCode() {
        return Integer.parseInt(this.get("code") + "");
    }

    public boolean isSuccess() {
        return Boolean.parseBoolean(this.get("result") + "");
    }
}
